<template>
  <el-dialog v-if="value" :visible="value" title="添加用户" width="50%" @close="$emit('input', false)">
    <el-form
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      :model="ruleForm"
      :rules="rules"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
      </el-form-item>

      <el-form-item label="密码" prop="checkPass">
        <el-input
          v-model="ruleForm.password"
          type="text"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item label="邮箱" prop="checkPass">
        <el-input
          v-model="ruleForm.email"
          type="text"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="mobile">
        <el-input v-model.number="ruleForm.mobile" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)">取 消</el-button>
      <el-button type="primary" @click="adduserInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addUserItem } from '@/api/user.js'

export default {
  props: {
    value: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      ruleForm: {
        username: '啃啃',
        password: '11',
        email: '',
        mobile: ''
      },
      rules: {
        username: [
          { required: true },
          { min: 3, max: 12, message: '长度在 3 到 12个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' },
          { pattern: '/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/' }
        ]

      }

    }
  },

  created() {},

  methods: {

    handleClose() {
      console.log(11)
    },
    // 添加用户
    async   adduserInfo() {
      try {
        const res = await addUserItem(this.ruleForm)
        console.log(res)
        this.$message.success('添加成功')
        this.$emit('input', false)
      } catch (err) {
        console.log(err)
      }
    }

  }
}
</script>

<style scoped lang="scss"></style>
